import { BaseOpt } from "./base";
import { ContentModel } from "../models";

export default class InsertOpt extends BaseOpt{
  
  private index = 0
  private data = ''
  
  constructor(index:number,data:string){
    super()
    this.index = index
    this.data = data
    this.type = 'InsertOpt'
  }

  apply(): void {
    this._model.insert(this.index,this.data)
    if(this._selection){
      this._selection.update(this.index + this.data.length, 0)
    }
  }

  reverse(): void {
    this._model.delete(this.index,this.data.length)
    if(this._selection){
      this._selection.update(this.index, 0)
    }
  }

  merge(opt: BaseOpt): boolean {
    if(opt.type!==this.type) return false
    if(this.isTimeout()) return false
    let insertOpt = opt as InsertOpt
    if(this.index + this.data.length !== insertOpt.index){
      return false
    }
    this.data += insertOpt.data
    return true
  }
}